// 2013-9-15下午03:19:20  net.betou.trade.admin.action.ConfigAct.java

package net.betou.trade.admin.action;

import static net.betou.common.page.SimplePage.cpn;
import static net.betou.common.page.SimplePage.cpnPageSize;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.betou.common.page.Pagination;
import net.betou.common.util.TradeUtils;
import net.betou.common.web.RequestUtils;
import net.betou.trade.action.abs.ActionSupport;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Config;
import net.betou.trade.main.manager.ConfigMng;
import net.betou.trade.web.Messages;
import net.betou.trade.web.ResponseUtil;
import net.betou.trade.web.WebErrors;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 站点配置
 * @author 叶装装
 */
@Controller
public class ConfigAct extends ActionSupport< Config >{

	@RequestMapping( "/config/v_list.jspx" )
	public String list(  Integer pageSize, Integer pageNo, String searchProperty, String searchValue, String orderProperty, String orderDirection, String filterStatus, HttpServletRequest request, HttpServletResponse response, ModelMap model  ){
		
		pageSize = cpnPageSize( pageSize );
		pageNo = cpn( pageNo );
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request ); // 获取当前管理员
		Pagination pagination = manager.getPage( searchProperty, searchValue, orderProperty, orderDirection, filterStatus, pageNo, pageSize );
		
		model.addAttribute( "admin", admin );
		paramToModel( model, pagination, null, pageSize, searchProperty, searchValue, orderProperty, orderDirection, null );
		
		return getListPage( );
	}
	
	@RequestMapping( "/config/v_edit.jspx" )
	public String v_edit( Integer id, HttpServletRequest request, ModelMap model ){
		WebErrors errors = validateUpdate( id );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		model.addAttribute( "config", manager.findById( id ) );
		model.addAttribute( "edit", true );
		return getUpdatePage( );
	}
	
	@RequestMapping( "/config/v_config.jspx" )
	public String v_config( HttpServletRequest request, ModelMap model ){
		AgentAndAdmins admin = TradeUtils.getAdmin( request ); // 获取当前管理员
		model.addAttribute( "config", manager.findConfig( admin ) );
		return getUpdatePage( );
	}
	
	/**
	 * 不进行图片的删除，对于这点图片资源还是耗得起的,
	 * @param id 该项应始终不为空
	 * @param edit 参数主要用于判断是v_edit.jspx传递过来的还是 v_config.jspx传递过来的
	 * @param pic 该参数名称只能为pic, 否则就要修改swfupload
	 * @return
	 */
	@RequestMapping( "/config/o_config.jspx" )
	public String o_config( Integer configId, Boolean edit, Config bean, String name, String pic, String oldPic, HttpServletRequest request, ModelMap model ){
		
		AgentAndAdmins admin = TradeUtils.getAdmin( request ); 
		
		WebErrors errors = validateUpdate( configId );
		
		if( errors.hasErrors( ) ){
			return errors.showErrorPage( model );
		}
		
		Config config = manager.findById( configId );
		
		pic = StringUtils.isNotBlank( pic ) ? pic : oldPic;	// 是使用新的，还是旧的
		
		String redirect;
		int saveOrUpdate = 1;
		
		if( Boolean.TRUE.equals( edit ) ){
			redirect = "v_edit.jspx?id=" + configId;
			
			config = manager.update( config, name, pic );
			
		}else{
			redirect = "v_config.jspx";
			
			if( config.getConfigDefault( ) && !admin.getSystem( ) ){
				String ip = RequestUtils.getIpAddr( request );
				bean.setLogo( pic ); // 设置logo
				config = manager.save( bean, admin, ip );
				saveOrUpdate = 0;
				
			}else{
				config = manager.update( config, name, pic );
			}
			
		}
		
		logMng.operating( saveOrUpdate, request, Messages.LOG_CONFIG, "id=" + config.getId( ) + ";name=" + config.getName( ) );
		
		return "redirect:" + redirect;
	}
	
	
	@RequestMapping( "/config/o_delete.jspx" )
	public void o_delete( Integer [ ] ids, HttpServletResponse response, HttpServletRequest request ){
		
		ResponseUtil util = validateDelete( ids, response, request );

		if( util.hasResponse( ) ){
			util.renderJson( );
			return;
		}

		Config [ ] beans = manager.deleteByIds( ids );

		for ( Config bean : beans ){
			logMng.operating( 2, request, Messages.LOG_CONFIG, "id=" + bean.getId( ) + ",name=" + bean.getName( ) );
		}

		util.addSuccessResponse( Messages.getDeleteSuccessMessage( beans.length ) );

		util.renderJson( );
		return;
		
	}
	
	private WebErrors validateUpdate( Integer id ){
		
		WebErrors errors = WebErrors.create( );
		
		if( id == null || id <= 0 ){
			errors.addError( Messages.DATA_NOT_EXIST );
			return errors;
		}
		
		Config config = manager.findById( id );
		
		if( config == null ){
			errors.addError( Messages.DATA_NOT_EXIST );
		}
		
		return errors;
	}
	
	private ResponseUtil validateDelete( Integer [ ] ids,  HttpServletResponse response, HttpServletRequest request ) {
		ResponseUtil util = ResponseUtil.create( response );

		if( ids == null || ids.length == 0 ){
			util.addErrorResponse( Messages.DATA_NOT_SELECT );
			return util;
		}

		for ( Integer id : ids ){
			Config bean = manager.findById( id );
			
			if( bean == null ){
				util.addErrorResponse(  Messages.DATA_NOT_EXIST  );
				break;
			}
		}
		return util;
	}
	
	@Autowired
	private ConfigMng manager;
	
	public static final String DIRECTORY = "config";
	@Override
	protected String getDirectory( ) {
		return DIRECTORY;
	}

}
